
package org.lightpersist.util;

import java.lang.reflect.Method;

import net.jcip.annotations.Immutable;

import org.aopalliance.intercept.MethodInterceptor;

import com.google.inject.cglib.proxy.InvocationHandler;
import com.google.inject.matcher.Matcher;

@Immutable
class AopAllianceJdkProxyAdapter implements InvocationHandler {
	private final Matcher<? super Class<?>> typeMatcher;
    private final Matcher<? super Method> methodMatcher;
    private final MethodInterceptorChain chain;
    
    public AopAllianceJdkProxyAdapter(Matcher<? super Class<?>> typeMatcher,
			Matcher<? super Method> methodMatcher,
			MethodInterceptor[] interceptors) {
    	this.typeMatcher = typeMatcher;
    	this.methodMatcher = methodMatcher;
    	this.chain = new MethodInterceptorChain(interceptors);
	}

	public Object invoke(final Object object, final Method method, final Object[] objects) throws Throwable {
		if(typeMatcher.matches(object.getClass()) && methodMatcher.matches(method)){
			return chain.invoke(object, method, objects,false);
		}else{
			return method.invoke(object, objects);
		}
    }
}
